import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './editlabelmedical.css'
import { AtInput, AtButton, AtIcon, AtList, AtListItem, AtSwipeAction } from 'taro-ui'
import app from '../../../../main.js'

export default class EditLabelMedical extends Component {

    config = {
        navigationBarTitleText: '编辑病历标签'
    }
    constructor() {
        super(...arguments);
        this.state = {
            label_name: '',
            receive_array:[],
            arrId:this.$router.params.id,
            boolean:'false',
            idsArr:[],
            idsGet:[],
        }
    }

    //页面开始执行的方法，判断是否为新建标签
    Start(){
        let me = this;
        if(me.$router.params.id==0 || me.$router.params.arrId==0){
            me.setState({
                boolean:'true'
            })
        }else{
            me.setState({
                boolean:'false'
            })
        }
    }

    //页面刷新执行的方法，判断数值是从哪个页面传回来的
    Restart(){
        let me = this;
        if(me.$router.params.id===undefined){
            me.receive();
        }else{
            me.getId();
        }
    }

    //从选择标签页面接收的数据
    receive(){
        this.setState({
            arrId:this.$router.params.arrId,
            label_name:this.$router.params.label_name
        })
        if(this.$router.params.arr===undefined){
            console.log('无数据')
        }else{
            let me = this;
            let arr = JSON.parse(this.$router.params.arr)
            let arrs = JSON.parse(this.$router.params.ids) 
            me.setState({
                receive_array:arr,
                idsGet:arrs
            })
        }          
    }

    //获取第一个页面传过来的id值，并且赋予相应的数据
    getId(){
        let me = this;
        let arr = [];
        let data={
            label_id:this.$router.params.id
        }
        let iarr = [];
        if(me.$router.params.id==0){
            this.setState({
                label_name:' '
            })
        }else{
            app.get('web/caselabel/cases',data,function(res){
                console.log(res)
                arr = res.data.result
                for(var i=0;i<arr.length;i++){
                    iarr.push(arr[i].id)
                }
                me.setState({
                    receive_array:arr,
                    label_name:me.$router.params.name,
                    idsArr:iarr
                },()=>{

                })           
            })
        }
    }

    componentWillMount () {       

    }

    componentDidShow() {
        this.Start();
        this.Restart();
    }

    //清除输入框文字按钮
    clearText(){
        this.setState({
            label_name: ''
        })
    }

    //输入框
    handleChange(label_name) {
        this.setState({
            label_name
        })
    }

    //保存标签按钮
    updateLabel() {
        let me = this;
        let data = {};

        if(me.state.label_name.trim()===''){
            app.alertTab('标签名称不能为空');
            return;
        }
        // if(me.state.receive_array.length===0){
        //     app.alertTab('标签下的病历不能为空');
        //     return;
        // }

        //第一次判断：标签是否为新建标签
        if(me.state.boolean=='true'){
            data={
                token:'sss',
                label_name:me.state.label_name,
                ids:me.state.idsGet
            }
            app.post('web/caselabel/save', data, function(res){
                app.redirectTo('../labelmanagemedical/labelmanagemedical')
            })
        }else{
            //第二次判断：如果不是新建标签，标签里的内容是从哪里传来的
            if(me.$router.params.arr===undefined){
                console.log(me.$router.params.id)
                data={
                    token:'sss',
                    label_name:me.state.label_name,
                    id:me.$router.params.id,
                    ids:me.state.idsArr
                }
                app.post('web/caselabel/save', data, function(res){
                    app.redirectTo('../labelmanagemedical/labelmanagemedical')
                })
            }else{
                console.log(me.$router.params.arrId)
                data={
                    token:'sss',
                    label_name:me.state.label_name,
                    id:me.$router.params.arrId,
                    ids:me.state.idsGet
                }
                app.post('web/caselabel/save', data, function(res){
                    app.redirectTo('../labelmanagemedical/labelmanagemedical')
                })
            }
        }
    }
    
    navigateTo(url) {
        app.redirectTo(url);
    }

    //删除一个标签
    deleteOption(id,idx){
        let me = this;
        let arr = [];
        let Bids = [];
        let Iids = [];

        if(me.$router.params.arr===undefined){
            arr = me.state.receive_array;
            Bids = me.state.idsArr;
            
            arr.splice(idx,1)
            Bids.splice(idx,1)

            me.setState({
                receive_array:arr,
                idsArr:Bids,
        })
        }else{
            arr = me.state.receive_array;
            Iids = me.state.idsGet
            
            arr.splice(idx,1)
            Iids.splice(idx,1)

            me.setState({
                receive_array:arr,
                idsGet:Iids
            })      
        }
    }

    //删除病历标签
    deleteLabel() {
        let me = this;
        Taro.showModal({
            title: '提示',
            content: '确认删除',
        }).then(res=>{
            if(res.confirm){
                if (me.$router.params.id == '0') {
                    app.redirectTo('../labelmanagemedical/labelmanagemedical');
                } else {
                    var data = {};
                    data.label_id = me.state.arrId
                    app.post("web/caselabel/delete", data, function (res) {
                        console.log(res);
                        app.redirectTo('../labelmanagemedical/labelmanagemedical');
                    });
                }
            }
        })  
    }


    render() {
        return (
            <View className='edit-label-medical'>

                <AtInput
                    className='tag-1'
                    editable={false}
                    name='value'
                    title='标签名称'
                    type='text'
                />
                <View className='inputBox'>
                    <AtIcon
                        value='close-circle'
                        className='closeIcon'
                        size='20'
                        color='#999999'
                        onClick={this.clearText.bind(this)}
                    >
                    </AtIcon>
                    <AtInput
                        className='input-a'
                        name='value'
                        type='text'
                        value={this.state.label_name}
                        onChange={this.handleChange.bind(this)}
                    />
                </View>
                <AtInput
                    className='tag-1'
                    editable={false}
                    name='value'
                    title='标签病历'
                    type='text'
                />

                <AtList>
                    {
                        this.state.receive_array.map((item, index) => {
                            return (
                                    <AtSwipeAction
                                        onClick={this.deleteOption.bind(this,item.id,index)}
                                        autoClose={false}
                                        options={[
                                            {
                                                text: '删除',
                                                style: {
                                                backgroundColor: '#FF4949'
                                                }
                                            }
                                        ]}>
                                        {/* <AtListItem
                                            className='medicals'
                                            title={item.patient_name}
                                            extraText={'就诊于' + item.visit_at}
                                            note={item.diagnosis}
                                        /> */}
                                        <View>
                                            <View className='box-a'>
                                                <View className='name'>{item.patient_name}</View>
                                                <View className='time'>{'就诊于：' + item.created_at}</View>
                                            </View>
                                            <View className='text'>{item.diagnosis == null ? none :item.diagnosis}</View>
                                            <View className='emptys'></View>
                                        </View>
                                    </AtSwipeAction>
                            )
                        })
                    }

                    <View className='tips' style={
                        this.state.receive_array.length === 0
                            ? 'display:block'
                            : 'display:none'
                    }>
                        该标签病历目前为空                
                    </View>
                    
                        <AtListItem
                            title='添加病历到标签'
                            hasBorder={false}
                            arrow='right'
                            onClick={this.navigateTo.bind(this, '../medicaltolabel/medicaltolabel?arr='+JSON.stringify(this.state.receive_array)+'&arrId='+this.state.arrId+'&label_name='+this.state.label_name)}
                            thumb={app.imageUri + 'img/tianjiabingli.png'}
                        />
                </AtList>

                <View className='empty'></View>

                <View className='btnBox'>
                    <View className='btn'>
                        <AtButton type='primary' className='red' onClick={this.deleteLabel.bind(this)}>删除标签</AtButton>
                    </View>
                    <View className='btn'>
                        <AtButton type='primary' className='green' onClick={this.updateLabel.bind(this)}>保存标签</AtButton>
                    </View>
                </View>    
            </View>
        )
    }
}
